package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import javax.script.Bindings;

/**
 * 监听队列获取消息
 *
 * @className:SpringRabbitMqListener
 * @date: 2025/7/7 下午4:37
 * @description:
 */
@Slf4j
@Component
public class SpringRabbitMqListener {
    //@RabbitListener(queues = {"simple.queue"})
    //public void listenSimpleQueueMessage(String message) {
    //    System.out.println("Simple Queue Message Received: " + message);
    //}

    @RabbitListener(queues = {"work.queue"})
    public void listenworkQueueMessage(String message) throws InterruptedException {
        System.out.println("work Queue Message Received: " + message);
        Thread.sleep(20);
    }

    @RabbitListener(queues = {"work.queue"})
    public void listenworkQueueMessage2(String message) throws InterruptedException {
        System.out.println("work Queue2 Message Received: " + message);
        Thread.sleep(200);
    }

    @RabbitListener(queues = {"fanout.queue1"})
    public void listenFanOutQueueMessage(String msg) throws InterruptedException {
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }

    @RabbitListener(queues = {"fanout.queue2"})
    public void listenFanOutQueueMessage2(String msg) throws InterruptedException {
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }

    @RabbitListener(queues = {"direct.queue1"})
    public void listenDirectQueueMessage(String msg) throws InterruptedException {
        System.out.println("消费者1接收到direct消息：【" + msg + "】");
    }

    @RabbitListener(queues = {"direct.queue2"})
    public void listenDirectQueueMessage2(String msg) throws InterruptedException {
        System.out.println("消费者2接收到direct消息：【" + msg + "】");
    }

    @RabbitListener(queues = {"topic.queue1"})
    public void listenTopicQueueMessage(String msg) throws InterruptedException {
        System.out.println("ChinaTopic接收到topic消息：【" + msg + "】");
    }

    @RabbitListener(queues = {"topic.queue2"})
    public void listenTopicQueueMessage2(String msg) throws InterruptedException {
        System.out.println("NewsTopic接收到topic消息：【" + msg + "】");
    }

    /**
     * 通过注解来声明队列
     *
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "topic.queue"),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = {"china.#", "#.news"})})
    public void listenTopicQueueMessage3(String msg) throws InterruptedException {
        System.out.println("NewsAndNewTopic接收到topic消息：【" + msg + "】");
    }

    //@RabbitListener(queuesToDeclare = @Queue(
    //        name = "lazy.queue",
    //        durable = "true",
    //        arguments = @Argument(name = "x-queue-mode",value = "lazy")
    //))
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = "lazy.queue", durable = "true", arguments = @Argument(name = "x-queue=mode", value = "lazy")),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = {"china.#", "#.news"})
    })
    public void listenLazyQueueMessage(String msg) throws InterruptedException {
        System.out.println("Lazy Queue Message Received: " + msg);
    }
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage2(String message) throws InterruptedException {
        log.info("消费者接收到消息：{}",message);
        if (true) {
            throw new RuntimeException("故意的");
        }
        log.info("消息处理完成");
    }

    @RabbitListener(bindings=@QueueBinding(value = @Queue(name = "delay.queue",durable = "true"),
            exchange =@Exchange(name = "delay.direct",delayed = "true"),key = "delay"))
    public void  listenDelayMessage(String msg) throws InterruptedException {
        log.info("接收到delay.queue的延迟消息：{}",msg);
    }
}
